package com.example.androidbighomework;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidbighomework.SQLiteHelper.MyHelper;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    EditText med_register_account,med_register_password,med_sure_password;
    Button mbtn_to_login,mbtn_zhuce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        med_register_account=findViewById(R.id.ed_register_account);
        med_register_password=findViewById(R.id.ed_register_password);
        med_sure_password=findViewById(R.id.ed_register_sure_password);
        mbtn_zhuce=findViewById(R.id.btn_zhuce);
        mbtn_to_login=findViewById(R.id.btn_to_login);
        mbtn_to_login.setOnClickListener(this);
        mbtn_zhuce.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_zhuce:
                String account = med_register_account.getText().toString().trim();
                String password = med_register_password.getText().toString().trim();
                String surepassword = med_sure_password.getText().toString().trim();
                if (TextUtils.isEmpty(account)){
                    Toast.makeText(this, "请输账号", Toast.LENGTH_SHORT).show();
                }else if(TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "请输密码", Toast.LENGTH_SHORT).show();
                }else if(!password.equals(surepassword)){
                    Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                } else {
                    //boolean isSaveSuccess = FileSave.savaUserInfo(this, account, password);
//                    boolean isSaveSuccess= SaveAccountPassword.saveUserInfo(this,account,password);
//                    if (isSaveSuccess){
//                        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
//                    }else {
//                        Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
//                    }
                    if(find(account)!=-1){
                        Toast.makeText(this, "账户已存在", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        insert(account,password);
                        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                }
                break;
            case R.id.btn_to_login:
                finish();
                break;
        }
    }
    //添加账户信息
    public void insert(String account,String password){
        MyHelper helper=new MyHelper(RegisterActivity.this);
        SQLiteDatabase db=helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("account",account);
        values.put("password",password);
        long id=db.insert("user",null,values);
        Log.i("insert","注册成功");
        Log.i("insertId", String.valueOf(id));
        db.close();
    }
    //根据账号查找
    public int find(String account){
        MyHelper helper=new MyHelper(RegisterActivity.this);
        SQLiteDatabase db=helper.getReadableDatabase();
        Cursor cursor=db.query("user",null,"account=?",new String[]{account+""},null,null,null);
        if(cursor.getCount()!=0){
            while (cursor.moveToNext()){
                Integer uid=cursor.getInt(cursor.getColumnIndex("u_id"));
                String Account=cursor.getString(cursor.getColumnIndex("account"));
                String Password=cursor.getString(cursor.getColumnIndex("password"));
                Log.i("Account",Account);
                return uid;
            }

        }
        cursor.close();
        db.close();
        return -1;
    }
}